package net.neptech.repository;

import net.neptech.model.appointment.ClnicStaff;
import net.neptech.model.vo.ClnicStaffVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 医生坐诊号表
 * @author ruirui

 */
public interface ClnicStaffRepository extends JpaRepository<ClnicStaff, Long> {

    /**
     * 查询某位医生在指定日期范围内的坐诊信息
     * @param staffId 医生ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 坐诊信息列表
     */
    @Query("SELECT cs FROM ClnicStaff cs WHERE cs.staffId = :staffId " +
            "AND cs.typeId = :typeId AND cs.clnicTime BETWEEN :startDate AND :endDate")
    List<ClnicStaff> findDoctorAvailability(
            @Param("typeId") Integer typeId,
            @Param("staffId") Long staffId,
            @Param("startDate") Date startDate,
            @Param("endDate") Date endDate
    );

    /**
     * 查询所有医生
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 坐诊信息列表
     */
    @Query("SELECT cs FROM ClnicStaff cs WHERE  cs.clnicTime BETWEEN :startDate AND :endDate")
    List<ClnicStaff> findDoctorAvailabilityByWeek(
            @Param("startDate") Date startDate,
            @Param("endDate") Date endDate
    );
    @Modifying
    @Transactional
    @Query("update ClnicStaff cs set cs.slotUse = cs.slotUse + :num where cs.id = :id")
    Integer updateSlotById(@Param("id") Long id,@Param("num") Integer num);

    List<ClnicStaff> getClnicStaffsByDeptId(Long deptId);


    @Query("SELECT cs FROM ClnicStaff cs " +
            "WHERE cs.clnicTime >= :startDate " +
            "AND cs.clnicTime < :endDate " +
            "AND cs.typeId = :typeId " +
            "AND cs.deptId = :deptId")
    List<ClnicStaff> findClinicStaffByDateAndDeptId(
            @Param("startDate") Date startDate,
            @Param("endDate") Date endDate,
            @Param("deptId") Long deptId,
            @Param("typeId") Byte typeId);


    @Query("SELECT cs FROM ClnicStaff cs " +
            "WHERE cs.clnicTime >= :startDay " +
            "AND cs.clnicTime < :endDay " +
            "AND cs.typeId = 1 " +
            "AND cs.staffId = :staffId")
    List<ClnicStaff> findClnicStaffsByClnicTime(@Param("staffId") Long staffId,
                                                  @Param("startDay") Date startDay,
                                                  @Param("endDay")Date endDay);
}
